define(function(require, exports, module) {
	require('./css/style.css')

	function initialize() {
		var dataURL = require('./config.json');

		var fileUpload = require("./html/FileUpload/Widget.js").onload(dataURL);
		var imageUpload = require("./html/ImageUpload/Widget.js").onload(dataURL);
		var imageCutUpload = require("./html/ImageCutUpload/Widget.js").onload(dataURL);
		return {
			components: {
				"file-upload": fileUpload,
				"image-upload": imageUpload,
				"image-cut-upload": imageCutUpload
			},
			template: require('./Widget.html'),
			props: {
				value: {
					type: String,
					default: ''
				},
				// 图片路径
				path: {
					type: String,
					default: 'common'
				},
				//文件上传类型: 'img', 'img-cut', 'file'
				fileType: {
					type: String,
					default: 'img'
				},
				is_more:{
					type: Boolean,
					default:false
				},
				// 大小限制(MB)
				fileSize: {
				    type: Number,
				    default: 5,
				},
				// 弹窗模式
				dialog: {
				    type: Boolean,
				    default: true,
				},
				// 是否多图
				multiple: {
				    type: Boolean,
				    default: true,
				}
			},
			data() {
				return {
					imageUrl: '',
					imageUrls:[],
					show: false,
					// 按钮loading
					buttonLoading: false,
					// 遮罩层
					loading: true,
					// 导出遮罩层
					exportLoading: false,
					// 选中数组
					// ids: [],
					// 非单个禁用
					single: true,
					// 非多个禁用
					// multiple: true,
					// 显示搜索条件
					showSearch: true,
					// 总条数
					total: 0,
					// OSS对象存储表格数据
					ossList: [],
					// 弹出层标题
					title: "",
					ossLabel: "",
					// 弹出层标题
					type: 0,
					// 是否显示弹出层
					open: false,
					// 预览列表图片
					previewListResource: true,
					// 创建时间时间范围
					dateRange: [],
					// 查询参数
					queryParams: {
						pageNum: 1,
						pageSize: 8,
						fileName: undefined,
						originalName: undefined,
						fileSuffix: undefined,
						url: undefined,
						createTime: undefined,
						createBy: undefined,
						service: this.path
					},
					// 表单参数
					form: {},
					// 表单校验
					rules: {
						file: [{
							required: true,
							message: "文件不能为空",
							trigger: "blur"
						}]
					}
				};
			},
			created() {
				this.getList();

			},
			watch: {
				value: {
					handler(val) {
						if (val) {
							if(this.is_more){
								this.imageUrls = [];
								if(val.indexOf(",") === -1){
									this.imageUrls.push(val);
								}else{
									this.imageUrls = val.split(",");
								}
							}else{
								this.imageUrl = alluse.oss + val
							}
						} else {
							this.imageUrl = ""
						}
						return val
					},
					deep: true,
					immediate: true
				}
			},
			methods: {
				imageUrlsRemove(index){
					this.$delete(this.imageUrls,index)
					this.$emit("input", this.imageUrls.join(","));
				},
				/** 查询OSS对象存储列表 */
				getList() {
					this.loading = true;
					$.ajax({
						url: alluse.bigcenter + dataURL.查询OSS对象存储列表,
						type: 'GET',
						dataType: 'json',
						contentType: 'application/json',
						data: this.addDateRange(this.queryParams, this.dateRange),
						success: (response) => {
							this.ossList = response.rows;
							this.total = response.total;
							this.loading = false;
						}
					});
				},
				checkFileSuffix(fileSuffix) {
					let arr = ["png", "jpg", "jpeg"];
					return arr.some(type => {
						return fileSuffix.indexOf(type) > -1;
					});
				},
				// 取消按钮
				cancel() {
					this.open = false;
					this.reset();
				},
				// 表单重置
				reset() {
					this.form = {
						file: undefined,
					};
					this.resetForm("form");
				},
				/** 搜索按钮操作 */
				handleQuery() {
					this.queryParams.pageNum = 1;
					this.getList();
				},
				/** 重置按钮操作 */
				resetQuery() {
					this.daterangeCreateTime = [];
					this.resetForm("queryForm");
					this.handleQuery();
				},
				// 多选框选中数据
				// handleSelectionChange(selection) {
				// 	this.ids = selection.map(item => item.ossId)
				// 	this.single = selection.length !== 1
				// 	this.multiple = !selection.length
				// },
				cardShow() {
					this.show = true;
					this.queryParams.pageNum = 1;
					this.getList();
				},
				cardClick(item) {
					this.show = false;
					if(this.is_more){
						this.imageUrls.push("/" + item.service + "/" + item.fileName);
						this.$emit("input", this.imageUrls.join(","));
					}else{
						this.$emit("input", "/" + item.service + "/" + item.fileName);
						this.imageUrl = item.url;
					}

				},
				/** 任务日志列表查询 */
				handleOssConfig() {
					/* this.$router.push({
						path: '/system/oss-config/index'
					}) */
					this.$refs.config.getShow();
				},
				/** 文件按钮操作 */
				handleFile() {
					this.reset();
					this.open = true;
					this.title = "上传文件";
					this.ossLabel = "文件";
				},
				/** 图片按钮操作 */
				handleImage() {
					this.reset();
					this.open = true;
					this.title = "上传图片";
					this.ossLabel = "图片";
				},
				/** 提交按钮 */
				submitForm() {
					this.open = false;
					this.getList();
				},
				/** 下载按钮操作 */
				handleDownload(row) {
					this.download.oss(row.ossId)
				},
				/** 删除按钮操作 */
				handleDelete(row) {
					const ossIds = row.ossId;
					this.$confirm('是否确认删除OSS对象存储编号为"' + ossIds + '"的数据项?').then(() => {
						this.loading = true;
						return $.ajax({
							url: alluse.bigcenter + dataURL.删除OSS对象存储 + ossIds,
							type: 'DELETE',
							dataType: 'json',
							contentType: 'application/json',
							success: (response) => {
								if (response.code === 200) {

								}
							}
						});
					}).then(() => {
						this.loading = false;
						this.getList();
						this.msgSuccess("删除成功");
					}).finally(() => {
						this.loading = false;
					});
				},
				// 预览列表图片状态修改
				handlePreviewListResource(previewListResource) {
					let text = previewListResource ? "启用" : "停用";
					this.$confirm('确认要"' + text + '""预览列表图片"配置吗?').then(() => {
						return this.updateConfigByKey("sys.oss.previewListResource",
							previewListResource);
					}).then(() => {
						this.getList()
						this.$modal.msgSuccess(text + "成功");
					}).catch(() => {
						this.previewListResource = previewListResource !== true;
					})
				}
			}
		}
	}

	module.exports = {
		onload: initialize
	};
});
